﻿using Developer.Core.FrameWork.ClusterLoadBalance.Extensions;
using Developer.Core.FrameWork.DynamicMiddlePlatformService.Impl;
using Developer.Core.FrameWork.DynamicMiddlePlatformService.Options;
using Developer.Core.FrameWork.DynamicMiddlePlatformService.Service;
using Developer.Core.FrameWork.MiddlePlatformService.Extensions;
using Developer.Core.FrameWork.RegisterCenter.Extensions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Developer.Core.FrameWork.DynamicMiddlePlatformService.Extensions
{
    /// <summary>
    /// 动态中台扩展类
    /// </summary>
    public static class DynamicMiddlePlatformCollectionExtensions
    {
        public static IServiceCollection AddDynamicMiddlePlatform<TMiddlePlatformService,TMiddlePlatformImplementation>(this IServiceCollection services)
            where TMiddlePlatformService:class
            where TMiddlePlatformImplementation:class, TMiddlePlatformService
        {


            return services;
        }

        public static IServiceCollection AddDynamicMiddlePlatform<TMiddlePlatformService, TMiddlePlatformImplementation>(this IServiceCollection services,Action<DynamicMiddlePlatformOptions> options)
            where TMiddlePlatformService : class
            where TMiddlePlatformImplementation : class, TMiddlePlatformService
        {
            DynamicMiddlePlatformOptions dynamicMiddlePlatformOptions = new DynamicMiddlePlatformOptions();
            options(dynamicMiddlePlatformOptions);

            // 1、注册服务发现
            services.AddServiceDiscovery(dynamicMiddlePlatformOptions.discoveryOptions);

            // 2、注册负载均衡
            services.AddLoadBalanceService(dynamicMiddlePlatformOptions.loadOption);

            // 3、添加中台服务
            services.AddMiddlePlatformService(dynamicMiddlePlatformOptions.platformOption);

            // 4、注册动态中台Url服务
            services.AddSingleton<IDynamicMiddlePlatformUrl, DefaultDynamicMiddlePlatformUrl>();

            // 5、注册动态服务
            services.AddSingleton<TMiddlePlatformService, TMiddlePlatformImplementation>();

            return services;
        }
    }
}
